####### 
####### SCRIPT FOR: 
####### The Real Threat of Populism 
####### Implications of Winning and Losing 
####### for Satisfaction with Democracy among Populist Party Supporters


####### Overlaps: ESS data collection periods vs Government replacements and Elections in countries


rm(list=ls())
gc()


setwd("C:/Users/miros/Desktop/Research/Real Threat of Populism")


# LIBRARIES ---------------------------------------------------------------
library(haven)
library(tidyverse)
library(readxl)
library(RColorBrewer)





# LOADING DATA ------------------------------------------------------------

# _ European Social Survey ------------------------------------------------
# NOTE: Download from the ESS Data Portal: https://ess-search.nsd.no/
ESS <- read_sav("02-data/European-Social-Survey/ESS-Data-Wizard-subset-2023-06-27.sav")



# __ Interview dates ------------------------------------------------------

# Unifying the dates into a single column and format

# Rounds 1 through 2
ESS$int_start <- ifelse(ESS$essround <= 2, 
                        as.Date(paste(ESS$inwdd, ESS$inwmm, ESS$inwyr, sep = "/"), format = "%d/%m/%Y"),
                        NA)
ESS$int_end   <- ifelse(ESS$essround <= 2, 
                        as.Date(paste(ESS$inwdd, ESS$inwmm, ESS$inwyr, sep = "/"), format = "%d/%m/%Y"),
                        NA)

# Rounds 3 through 9
ESS$int_start <- ifelse(ESS$essround >= 3 & ESS$essround <= 9, 
                        as.Date(paste(ESS$inwdds, ESS$inwmms, ESS$inwyys, sep = "/"), format = "%d/%m/%Y"),
                        ESS$int_start)
ESS$int_end   <- ifelse(ESS$essround >= 3 & ESS$essround <= 9, 
                        as.Date(paste(ESS$inwdde, ESS$inwmme, ESS$inwyye, sep = "/"), format = "%d/%m/%Y"),
                        ESS$int_end)

# Round 10
ESS$int_start <- ifelse(ESS$essround == 10, 
                        as.Date(ESS$inwds, format = "%Y-%m-%d"),
                        ESS$int_start)
ESS$int_end   <- ifelse(ESS$essround == 10, 
                        as.Date(ESS$inwde, format = "%Y-%m-%d"),
                        ESS$int_end)


# Extracting earliest and latest ESS interviews
ESS_dates <-
  ESS %>%
  select(cntry, essround, int_start, int_end) %>%
  group_by(cntry, essround) %>%
  mutate(ESS_earliest = min(int_start, na.rm = TRUE)) %>%
  mutate(ESS_latest = max(int_start, na.rm = TRUE)) %>%
  mutate(country = sjlabelled::as_label(cntry)) %>%
  select(cntry, country, essround, ESS_earliest, ESS_latest) %>%
  unique() %>%
  filter_all(all_vars(!is.infinite(.))) %>%
  arrange(cntry)


# Adjusting date format
ESS_dates$ESS_earliest <- as.Date(ESS_dates$ESS_earliest, origin = "1970-01-01")
ESS_dates$ESS_latest <- as.Date(ESS_dates$ESS_latest, origin = "1970-01-01")





# _ ParlGov ---------------------------------------------------------------


# __ Elections ------------------------------------------------------------
# NOTE: Download 'ParlGov development version (13. March 2023)' at: https://www.parlgov.org/data-info/
ParlGov <- read_excel("02-data/ParlGov/parlgov.xlsx", sheet = "election")

# Harmonizing name of the Czech Republic
ParlGov$country_name <- ifelse(ParlGov$country_name == "Czech Republic", "Czechia", ParlGov$country_name)

# Extracting relevant data--i.e., election dates
election_dates <-
  ParlGov %>%
  filter(election_type == "parliament") %>%
  mutate(election_date = as.Date(election_date, format = "%Y-%m-%d", origin = "1970-01-01")) %>%
  filter(election_date > as.Date("2001-01-01", format = "%Y-%m-%d", origin = "1970-01-01")) %>%
  select(country_name, election_date) %>%
  unique()

# Interim cleanup
rm(ParlGov)


# __ Cabinets -------------------------------------------------------------
# NOTE: Download 'ParlGov development version (13. March 2023)' at: https://www.parlgov.org/data-info/
ParlGov <- read_excel("02-data/ParlGov/parlgov.xlsx", sheet = "cabinet")

# Harmonizing name of the Czech Republic
ParlGov$country_name <- ifelse(ParlGov$country_name == "Czech Republic", "Czechia", ParlGov$country_name)

# Extracting relevant data--i.e., government change dates
government_dates <- 
  ParlGov %>% 
  mutate(gov_start = as.Date(start_date, format = "%Y-%m-%d", origin = "1970-01-01")) %>%
  filter(gov_start > as.Date("2001-01-01", format = "%Y-%m-%d", origin = "1970-01-01")) %>%
  select(country_name, gov_start, cabinet_name) %>%
  unique()

# Interim cleanup
rm(ParlGov)



# Imposing all datasets on each other
all_dates <- 
  rbind(
    # ESS: Start of data collection
    data.frame(
      Type = c("ESS (fieldwork)"),
      Country = ESS_dates$country,
      Date = ESS_dates$ESS_earliest,
      Group = paste(ESS_dates$country, ESS_dates$essround, sep = " ")
    ),
    
    # ESS: End of data collection
    data.frame(
      Type = c("ESS (fieldwork)"),
      Country = ESS_dates$country,
      Date = ESS_dates$ESS_latest,
      Group = paste(ESS_dates$country, ESS_dates$essround, sep = " ")
    ),
    
    # Election data
    data.frame(
      Type = c("Elections (Legislative/Lower Chamber)"),
      Country = election_dates$country_name,
      Date = election_dates$election_date,
      Group = NA
    ),
    
    # Government change data
    data.frame(
      Type = c("Government change"),
      Country = government_dates$country_name,
      Date = government_dates$gov_start,
      Group = NA
    )
  )


# Removing empty countries (which are not included in all datasets)
all_dates <- subset(all_dates, Country != "Ukraine")
all_dates <- subset(all_dates, Country != "Kosovo")
all_dates <- subset(all_dates, Country != "Serbia")
all_dates <- subset(all_dates, Country != "Russian Federation")
all_dates <- subset(all_dates, Country != "Malta")
all_dates <- subset(all_dates, Country != "Japan")
all_dates <- subset(all_dates, Country != "Montenegro")
all_dates <- subset(all_dates, Country != "North Macedonia")
all_dates <- subset(all_dates, Country != "New Zealand")
all_dates <- subset(all_dates, Country != "Canada")
all_dates <- subset(all_dates, Country != "Australia")
all_dates <- subset(all_dates, Country != "Albania")


# Figure
ggplot(all_dates, aes(x = Date, y = Country, color = Type, group = Group)) +
  geom_line(aes(color = Type, group = Group, size = 1, alpha = 0.5), subset(all_dates, Type == "ESS (fieldwork)")) +
  geom_point(aes(color = Type, group = Type, alpha = 0.5), subset(all_dates, Type == "Elections (Legislative/Lower Chamber)" | Type == "Government change")) +
  scale_color_manual(values = c("indianred1", "black", "grey"),
                     breaks = c("ESS (fieldwork)", "Elections (Legislative/Lower Chamber)", "Government change")) +
  scale_x_date(date_breaks = "2 years", date_minor_breaks = "1 year",
               date_labels = "%Y") +
  coord_cartesian(xlim = c(as.Date("2001-01-01"), as.Date("2023-01-01")), ylim = c(0, 33), expand = FALSE, clip = "off") +
  scale_y_discrete(limits = rev) +
  guides(size = "none", alpha = "none") +
  labs(x = NULL, y = NULL) +
  theme(panel.grid = element_line(colour = "gray90"),
        panel.spacing.y = unit(1, "lines"),
        legend.position = "bottom",
        legend.title=element_blank(),
        legend.background = element_rect(colour = 'black', fill = 'white', linetype='solid'),
        legend.key = element_blank(),
        panel.background = element_rect(fill = "white", colour = "black"),
        plot.background = element_rect(fill = "white", colour = "transparent"),
        panel.border = element_rect(fill = "transparent", colour = "black"),
        axis.ticks.y = element_blank(),
        plot.margin = unit(c(0.5, 0.5, 0.5, 0.5),"cm"))


# Saving the output
ggsave(filename = "03-figures and models/ESS vs elections.png", width = 25, height = 15, units = "cm", dpi = 600)
